package apig.client.stud;

import java.util.*;

import apig.client.*;
import apig.client.serviface.*;
import apig.client.stud.*;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.rpc.*;
import com.google.gwt.user.client.ui.*;

public class StudDiscsView extends VerticalPanel {

	private UserData ud;
	private Map<Discipline, DisciplineWidget> discWidgets = new HashMap<Discipline, DisciplineWidget>();
	private StudContentPanel content;
	private LabSelectionListener lsl;	
	
	private MouseOverHandler moverh = new MouseOverHandler() {
		@Override
		public void onMouseOver(MouseOverEvent event) {
			LabWidget src = (LabWidget) event.getSource();
			src.setSelected(true);
		}
	};

	private MouseOutHandler mouth = new MouseOutHandler() {
		@Override
		public void onMouseOut(MouseOutEvent event) {
			LabWidget src = (LabWidget) event.getSource();
			src.setSelected(false);
		}
	};

	private MouseDownHandler mdownh = new MouseDownHandler() {
		@Override
		public void onMouseDown(MouseDownEvent event) {
			LabWidget src = (LabWidget) event.getSource();
			onLabSelected(src.getLab());
		}
	};

	public StudDiscsView(UserData ud, StudContentPanel content, LabSelectionListener lsl) {
		this.ud = ud;
		this.lsl = lsl;
		this.content = content;
		setWidth("100%");
		Label header = new Label("Лабы");
		HorizontalPanel headerContainer = new HorizontalPanel();
		header.setStyleName("stud_subject_list_header");
		headerContainer.add(header);
		headerContainer.setWidth("100%");
		headerContainer.setCellHorizontalAlignment(header, HasHorizontalAlignment.ALIGN_CENTER);
		setCellWidth(headerContainer, "100%");
		add(headerContainer);

		// get the discipline list
		Stubman.getStubman()
				.getService()
				.getStudDisciplines(ud.userId,
						new AsyncCallback<List<Discipline>>() {

							@Override
							public void onSuccess(List<Discipline> result) {
								onSuccessLoadDiscs(result);
							}

							@Override
							public void onFailure(Throwable caught) {
								System.out.println(caught.getMessage());
								onFailLoadDiscs();
							}
						});
	}

	private void onFailLoadDiscs() {
		System.out.println("on fail load discs");
	}

	private void onSuccessLoadDiscs(List<Discipline> disciplines) {
		for (Discipline d : disciplines) {
			DisciplineWidget dw = new DisciplineWidget(d);
			HorizontalPanel hp = new HorizontalPanel();
			hp.add(dw);
			dw.setWidth("100%");
			add(hp);
			hp.setWidth("100%");
			setCellWidth(hp, "100%");
			add(new HTML("<hr>"));
		}
		VerticalPanel glue = new VerticalPanel();
		glue.setHeight("100%");
		add(glue);
		setCellHeight(glue, "100%");
	}

	private class DisciplineWidget extends VerticalPanel {

		private Discipline d;

		public DisciplineWidget(Discipline d) {
			this.d = d;
			Label discLabel = new Label(d.name);
			add(discLabel);
			// load the labs of discipline
			Stubman.getStubman()
					.getService()
					.getDisciplineLabs(d.discId,
							new AsyncCallback<List<Lab>>() {

								@Override
								public void onFailure(Throwable caught) {
									System.out.println(caught.getMessage());
									onFailLoadLabs();
								}

								@Override
								public void onSuccess(List<Lab> result) {
									onSuccessLoadLabs(result);
								}
							});
		}

		private void onSuccessLoadLabs(List<Lab> result) {
			for (Lab l : result) {
				LabWidget lw = new LabWidget(l, moverh, mouth, mdownh);
				add(lw);
			}
		}

		private void onFailLoadLabs() {
			System.out.println("on fail load labs");
		}
	}

	private void onLabSelected(Lab lab) {
		System.out.println("LAB SELECTED: " + lab.labId);
		lsl.onLabSelected(lab.labId);
		content.setLabInBrowser(lab);
	}

	private class LabWidget extends Button {

		private Lab lab;

		public LabWidget(Lab lab, MouseOverHandler moverh,
				MouseOutHandler mouth, MouseDownHandler mdownh) {
			this.lab = lab;
			this.setText(lab.labName);
			addMouseOverHandler(moverh);
			addMouseOutHandler(mouth);
			addMouseDownHandler(mdownh);
			setSelected(false);
		}

		public void setSelected(boolean b) {
			if (b) {
				setStyleName("selected_link_widget");
			} else {
				setStyleName("deselected_link_widget");
			}
		}

		public Lab getLab() {
			return lab;
		}
	}
}
